#!/usr/bin/env python
# -*- coding: utf-8 -*-

"""
    Setting Module here.

"""
import time, sys, json, wx
import urllib2, thread
from util import *
import config

class Pane_setting( wx.Panel ):
    def __init__(self, parent):
        wx.Panel.__init__( self, parent )
        self.accountcb = wx.ComboBox(self, -1, '', (-1, -1), (-1, -1), [], wx.CB_DROPDOWN|wx.CB_READONLY)
        self.SBox_1 = wx.StaticBox(self, -1, u"帐号")
        self.SBox_1_sizer = wx.StaticBoxSizer(self.SBox_1, wx.VERTICAL)
        self.SBox_1_sizer.Add(self.accountcb, 0, wx.ALIGN_CENTER_HORIZONTAL| wx.ALL, 5)

        self.seedoption = wx.CheckBox(self, -1, u'自动播种')
        self.seedoption.SetValue(True)
        self.seedcb = wx.ComboBox(self, -1, '', (-1, -1), (-1, -1), [], wx.CB_DROPDOWN|wx.CB_READONLY)
        self.SBox_2 = wx.StaticBox(self, -1, u"种地")
        self.SBox_2_sizer = wx.StaticBoxSizer(self.SBox_2, wx.VERTICAL)
        self.SBox_2_sizer.Add(self.seedoption, 0,  wx.ALIGN_CENTER_HORIZONTAL| wx.ALL, 5)
        self.SBox_2_sizer.Add(self.seedcb, 0, wx.ALIGN_CENTER_HORIZONTAL| wx.ALL, 5)
        self.Bind(wx.EVT_CHECKBOX, self.Evtseedoption, self.seedoption)

        self.button_all = wx.Button(self, -1, u"全选")
        self.button_clear = wx.Button(self, -1, u"清空")
        self.SBox_3 = wx.StaticBox(self, -1, u"偷还是不偷")
        t = wx.StaticText(self, -1, u"选择想偷的")
        self.steallist = wx.CheckListBox(self, -1, (-1,-1), (-1,-1), [])
        self.steallist_ranch = wx.CheckListBox(self, -1, (-1,-1), (-1,-1), [])
        sizer_steal_button = wx.BoxSizer(wx.HORIZONTAL)
        sizer_steal_button.Add(self.button_all, 0, 0, 0)
        sizer_steal_button.Add(self.button_clear, 0, 0, 0)
        self.SBox_3_sizer = wx.StaticBoxSizer(self.SBox_3, wx.VERTICAL)
        self.SBox_3_sizer.Add(t, 0, wx.ALIGN_CENTER_HORIZONTAL| wx.ALL, 5)
        self.SBox_3_sizer.Add(sizer_steal_button, 0, wx.ALIGN_CENTER_HORIZONTAL| wx.ALL, 5)
        sizer_steallist = wx.BoxSizer(wx.HORIZONTAL)
        sizer_steallist.Add(self.steallist, 0, wx.ALIGN_CENTER_HORIZONTAL| wx.ALL, 5)
        sizer_steallist.Add(self.steallist_ranch, 0, wx.ALIGN_CENTER_HORIZONTAL| wx.ALL, 5)

        self.SBox_3_sizer.Add(sizer_steallist, 0, 0,0)

        self.Bind(wx.EVT_CHECKLISTBOX, self.Evtsteallist, self.steallist)
        self.Bind(wx.EVT_CHECKLISTBOX, self.Evtsteallist_ranch, self.steallist_ranch)
        self.Bind(wx.EVT_BUTTON, self.EvtSelectNone, self.button_clear)
        self.Bind(wx.EVT_BUTTON, self.EvtSelectAll, self.button_all)

        self.SBox_4 = wx.StaticBox(self, -1, u"日志")
        self.logoption = wx.CheckBox(self, -1, u'保存日志')
        self.SBox_4_sizer = wx.StaticBoxSizer(self.SBox_4, wx.VERTICAL)
        self.SBox_4_sizer.Add(self.logoption, 0, wx.ALIGN_CENTER_HORIZONTAL| wx.ALL, 5)
        self.Bind(wx.EVT_CHECKBOX, self.Evtlogoption, self.logoption)

        self.timeroption = wx.CheckBox(self, -1, u'我还想要')
        self.timerTC = wx.TextCtrl(self, -1, '',(-1,-1), (50,-1))
        self.timernote = wx.StaticText(self, -1, u"分钟一次")
        timerTC_sizer = wx.BoxSizer(wx.HORIZONTAL)
        timerTC_sizer.Add(self.timerTC, 1, 0, 0)
        timerTC_sizer.Add(self.timernote, 1, 0, 0)
        self.SBox_timer = wx.StaticBox(self, -1, u"循环执行")
        self.SBox_timer_sizer = wx.StaticBoxSizer(self.SBox_timer, wx.VERTICAL)
        self.SBox_timer_sizer.Add(self.timeroption, 0,  wx.ALIGN_CENTER_HORIZONTAL| wx.ALL, 5)
        self.SBox_timer_sizer.Add(timerTC_sizer, 0, wx.ALIGN_CENTER_HORIZONTAL| wx.ALL, 5)
        self.Bind(wx.EVT_CHECKBOX, self.Evttimeroption, self.timeroption)

        self.delaySC = wx.SpinCtrl(self, -1, "", wx.DefaultPosition, (50, -1), 0, min = 0, max = 30)
        self.delaynote = wx.StaticText(self, -1, u"秒")
        delayTC_sizer = wx.BoxSizer(wx.HORIZONTAL)
        delayTC_sizer.Add(self.delaySC, 1, 0, 0)
        delayTC_sizer.Add(self.delaynote, 0, 0, 0)
        self.SBox_delay = wx.StaticBox(self, -1, u"连接延迟")
        self.SBox_delay_sizer = wx.StaticBoxSizer(self.SBox_delay, wx.HORIZONTAL)
        self.SBox_delay_sizer.Add(delayTC_sizer, 0, wx.ALIGN_CENTER_HORIZONTAL| wx.ALL, 5)
        self.Bind(wx.EVT_SPINCTRL, self.Evtdelay, self.delaySC)

        self.friends_choice = wx.RadioBox(
                self, -1, u"好友选择", wx.DefaultPosition, wx.DefaultSize,
                [u'全部好友',u'部分好友（已成熟，可偷）'], 1, wx.RA_SPECIFY_COLS
                )
        self.Bind(wx.EVT_RADIOBOX, self.Evtfriendschoice, self.friends_choice)

        self.button_save = wx.Button(self, -1, u"保存设置")
        self.Bind(wx.EVT_BUTTON, self.Evtsaveconfig, self.button_save)

        sizer_1 = wx.BoxSizer(wx.HORIZONTAL)
        sizer_2 = wx.BoxSizer(wx.VERTICAL)
        sizer_3 = wx.BoxSizer(wx.VERTICAL)

        sizer_2.Add(self.SBox_1_sizer, 0, wx.ALIGN_CENTER_HORIZONTAL| wx.ALL, 10)
        sizer_2.Add(self.SBox_2_sizer, 0, wx.ALIGN_CENTER_HORIZONTAL| wx.ALL, 10)
        sizer_2.Add(self.SBox_4_sizer, 0, wx.ALIGN_CENTER_HORIZONTAL| wx.ALL, 10)
        sizer_2.Add(self.SBox_timer_sizer, 0, wx.ALIGN_CENTER_HORIZONTAL| wx.ALL, 10)
        sizer_2.Add(self.SBox_delay_sizer, 0, wx.ALIGN_CENTER_HORIZONTAL| wx.ALL, 10)


        sizer_3.Add(self.SBox_3_sizer, 0, wx.ALIGN_CENTER_HORIZONTAL| wx.ALL, 10)
        sizer_3.Add(self.friends_choice, 0, wx.ALIGN_CENTER_HORIZONTAL| wx.ALL, 10)
        sizer_3.Add(self.button_save, 0, wx.ALIGN_CENTER_HORIZONTAL| wx.ALL, 10)


        sizer_1.Add(sizer_2, 1, 0, 0)
        sizer_1.Add(sizer_3, 1, 0, 0)


        self.SetSizer(sizer_1)

        list = parent.accountinfo.userlist
        for elm in list:
            self.accountcb.Append(elm)
        self.accountcb.SetValue(list[0])
 
        self.__set_properties()

    def __set_properties(self):
        seeds = config.seeds
        for elm in seeds:
            if elm['name'] in [u'友谊花',u'爱情果']:
                continue
            self.seedcb.Append(elm['name'])
        for elm in config.fruits:
            self.steallist.Append(elm['name'])
            if elm['seedid'] in config.global_steallist:
                self.steallist.Check( self.steallist.FindString(elm['name']))
        for elm in config.ranch_list:
            self.steallist_ranch.Append(elm[0])
            if elm[1] in config.global_steallist_ranch:
                self.steallist_ranch.Check( self.steallist_ranch.FindString(elm[0]))
        self.seedoption.SetValue(config.global_autoseed)
        self.seedcb.SetValue(config.global_autoseedselection)
        self.logoption.SetValue(config.global_logoption)
        self.timeroption.SetValue(config.global_timeroption)
        self.timerTC.SetValue(config.global_timervalue)
        if self.timeroption.IsChecked():
            self.timerTC.Enable( True )
        else:
            self.timerTC.Enable( False )

        self.friends_choice.SetSelection(config.global_friends_choice)
        self.delaySC.SetValue(config.global_connet_delay)

    def Evtdelay(self, event):
        config.global_connet_delay = self.delaySC.GetValue()

    def Evtfriendschoice(self, event):
        config.global_friends_choice = self.friends_choice.GetSelection()

    def Evtseedoption(self, event):
        if event.IsChecked():
            self.seedcb.Enable( True )
        else:
            self.seedcb.Enable( False )

    def Evtlogoption(self, event):
        if event.IsChecked():
            config.global_logoption = True
        else:
            config.global_logoption = False
         
    def Evttimeroption(self, event):
        if event.IsChecked():
            self.timerTC.Enable( True )
        else:
            self.timerTC.Enable( False )

    def Evtsaveconfig(self, event):
        configxml = ET.Element('data')
        ET.SubElement(configxml, 'autoseed').text = str(self.seedoption.GetValue())
        ET.SubElement(configxml, 'autoseedselection').text = self.seedcb.GetValue()
        ET.SubElement(configxml, 'logoption').text = str(config.global_logoption)
        ET.SubElement(configxml, 'timeroption').text = str(self.timeroption.GetValue())
        ET.SubElement(configxml, 'timervalue').text = self.timerTC.GetValue()
        ET.SubElement(configxml, 'steallist').text = ','.join(config.global_steallist)
        ET.SubElement(configxml, 'steallist_ranch').text = ','.join(config.global_steallist_ranch)
        ET.SubElement(configxml, 'friendschoice').text = str(self.friends_choice.GetSelection())
        ET.SubElement(configxml, 'connection_delay').text = str(config.global_connet_delay)
        ET.SubElement(configxml, 'jobs_list').text = ','.join(config.global_jobs_list)
        ET.ElementTree(configxml).write('config.xml', encoding='UTF-8')

#        f = file('config.xml', 'w')
#        f.write(etree.tostring(confingxml, encoding='UTF-8', xml_declaration=True, pretty_print=True))
#        f.close()

    def Evtsteallist(self, event):
        index = event.GetSelection()
        if self.steallist.IsChecked(index):
            self.steallist.SetSelection(index)
        self.saveSteallist()

    def Evtsteallist_ranch(self, event):
        index = event.GetSelection()
        if self.steallist_ranch.IsChecked(index):
            self.steallist_ranch.SetSelection(index)
        self.saveSteallist()

    def EvtSelectAll(self, event):
        for i in range(self.steallist.GetCount()):
            self.steallist.Check(i, True)
        for i in range(self.steallist_ranch.GetCount()):
            self.steallist_ranch.Check(i, True)
        self.saveSteallist()

    def EvtSelectNone(self, event):
        for i in range(self.steallist.GetCount()):
            self.steallist.Check(i, False)
        for i in range(self.steallist_ranch.GetCount()):
            self.steallist_ranch.Check(i, False)
        self.saveSteallist()

    def saveSteallist(self):
        plant = []
        fruits = config.fruits
        for i in range(self.steallist.GetCount()):
            if self.steallist.IsChecked(i):
#                if fruits[i]['name'] == self.steallist.GetString(i):
                plant.append(fruits[i]['seedid'])
        config.global_steallist = plant
        animal = []
        for i in range(self.steallist_ranch.GetCount()):
            if self.steallist_ranch.IsChecked(i):
                animal.append(dict(config.ranch_list)[self.steallist_ranch.GetString(i)])
        config.global_steallist_ranch = animal


